﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/AdminPage.master" Inherits="WebManager.BasePage" %>

<%@ Import Namespace="TalentPools.Model" %>
<%@ Import Namespace="TalentPools.BLL" %>
<script runat="server">

    string session;
    Admin_LoginManager admin = new Admin_LoginManager();
    AdminClass a = new AdminClass();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!AdminLogin())
        {
            JsWindows("cookie过期,请重新的登录", "Admin_Login.aspx");
            return;
        }
        else
        {
            session = Request.Cookies["AdminUser"].Values["name"];
        }
        if (!IsPostBack)
        {
            ManageVoid();
            string qstr = QueryString("Action");
            switch (qstr)
            {
                case "Add":
                    if (!a.Exists_Purview(session, "2202"))
                    {
                        JsWindows("请确认你是否有该权限！", "AdminManager.aspx");
                        return;
                    }
                    addP.Visible = true;
                    Panel1.Visible = true;
                    manageP.Visible = false;
                    editP.Visible = false;
                    Button2.Visible = false;
                    break;
                case "Edit":
                    if (!a.Exists_Purview(session, "2203"))
                    {
                        JsWindows("请确认你是否有该权限！", "AdminManager.aspx");
                        return;
                    }

                    Button1.Visible = false;
                    Panel1.Visible = true;
                    EditVoid();
                    break;
                case "Del":
                    if (!a.Exists_Purview(session, "2204"))
                    {
                        JsWindows("请确认你是否有该权限！", "AdminManager.aspx");
                        return;
                    }
                    DelVoid();
                    break;
            }
        }
    }
    /// <summary>
    /// 修改管理员资料
    /// </summary>
    protected void EditVoid()
    {
        addP.Visible = false;
        manageP.Visible = false;
        editP.Visible = true;
        int id = Convert.ToInt32(QueryString("Id"));
        Admin_Login adl = admin.GetAdmin(session);
        Admin_Login adl2 = admin.GetAdmin(id);
        if (!adl.Purview.Contains("10000"))
        {
            if (adl2.Purview.Contains("10000"))
            {
                JsWindows("您无权修改超级管理员", "AdminManager.aspx");
                return;
            }

        }
        ViewState["EditId"] = id;
        //绑定权限CheckBox
        SetCheckBoxChecked(id);
        //string[] s_purview = a.getCheckBox(session).Split(',');
       
        Admin_Login login = new Admin_LoginManager().GetAdmin(id);
        EditID.Text = login.LoginName;
        EditEmail.Text = login.Email;
    }
    /// <summary>
    /// 删除管理员资料
    /// </summary>
    protected void DelVoid()
    {
        int id = Convert.ToInt32(QueryString("id"));
        if (!admin.DeleteAdmin(id))
        {
            return;
        }
        ManageVoid();
    }
    /// <summary>
    /// 管理员列表
    /// </summary>
    protected void ManageVoid()
    {
        addP.Visible = false;
        manageP.Visible = true;
        editP.Visible = false;
        if (!a.Exists_Purview(session, "2201"))
        {
            RP1.DataSource = admin.GetAdmin(session);
            RP1.DataBind();
        }
        else
        {
            RP1.DataSource = admin.GetAdmin();
            RP1.DataBind();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (!a.Exists_Purview(session, "2202"))
        {
            JsWindows("对不起，你没有该权限", "AdminManager.aspx");
            return;
        }
        Admin_Login ad = new Admin_Login();
        ad.LoginName = AddID.Text;
        string str = FormsAuthentication.HashPasswordForStoringInConfigFile(AddPWD.Text, "MD5");
        ad.LoginPwd = str.Replace("-", "");
        ad.RealName = AddName.Text;
        ad.Email = AddEMail.Text;
        // 获取页面上选择的CheckBox的值
        ad.Purview = setCheckBox();
        if (Page.IsValid == true)
        {
            if (admin.GetAdminCount(ad.LoginName) > 0)
            {
                Label1.Text = "已经有此管理者，请勿重复添加。";
                return;
            }
            else
            {
                if (admin.InsertAdmin(ad))
                {
                    JsWindows("添加成功！", "AdminManager.aspx");
                    return;
                }
            }
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (Page.IsValid == true)
        {
            string pwd = admin.GetPwd(session);
            string NewPwdStr = FormsAuthentication.HashPasswordForStoringInConfigFile(EditPWD.Text, "MD5");
            string NewEmail = EditEmail.Text;
            // 获得权限值
            string s_purview = setCheckBox();
            // 只有修改权限的话
            Admin_Login ad = new Admin_Login();
            ad.LoginName = session;
            ad.Id = Convert.ToInt32(ViewState["EditId"]);
            ad.Email = NewEmail;
            ad.LoginPwd = NewPwdStr;
            ad.Purview = s_purview;
            if (a.Exists_Purview(session, "2201") && !a.Exists_Purview(session, "2202,2203"))
            {
                if (EditPwd2.Text == "")
                {
                    if (admin.UpdateAdmin2(ad))
                        JsWindows("修改登陆者资料成功！", "AdminManager.aspx");
                }
                else
                {
                    string OldPwdStr = FormsAuthentication.HashPasswordForStoringInConfigFile(OldEditPwd.Text, "MD5");
                    if (OldPwdStr != pwd)
                    {
                        EditLabel1.Text = "原始密码不正确";
                        return;
                    }
                    if (admin.UpdateAdmin(ad))
                        JsWindows("修改登陆者成功！", "AdminManager.aspx");
                }
            }
            else if (a.Exists_Purview(session, "2201,2202,2203,2204"))
            {
                if (EditPwd2.Text == "")
                {
                    if (admin.UpdateAdmin3(ad))
                        JsWindows("修改成功！", "AdminManager.aspx");
                }
                else
                {
                    string OldPwdStr = FormsAuthentication.HashPasswordForStoringInConfigFile(OldEditPwd.Text, "MD5");
                    if (OldPwdStr != pwd)
                    {
                        EditLabel1.Text = "原始密码不正确";
                        return;
                    }
                    if (admin.UpdateAdmin1(ad))
                        JsWindows("修改成功！", "AdminManager.aspx");
                }
            }
            else
            {
                JsWindows("请检查是否具有该权限", "AdminManager.aspx");
                return;
            }
        }
    }
    public string sf()
    {
        string sfstr = Eval("Purview").ToString();
        if (sfstr.Contains("10000"))
        {
            return "超级管理员";
        }
        else if (sfstr.Contains("2201,2202,2203,2204"))
        {
            return "高级管理员";
        }
        else
        {
            return "普通管理员";
        }
    }
    /// <summary>
    /// 获得该页面上所有的CheckBox的值
    /// </summary>
    /// <returns></returns>
    protected string setCheckBox()
    {
        string str = null;
        foreach (Control contorl in Panel1.Controls)
        {
            if (contorl is HtmlInputCheckBox && ((HtmlInputCheckBox)contorl).Checked)
            {
                str += ((HtmlInputCheckBox)contorl).Value + ",";
            }
        }
        if (str != null)
            return str.Substring(0, str.Length - 1);
        else
            return "";
    }
    /// <summary>
    /// 设置页面上所有的CheckBox的状态
    /// </summary>
    protected void SetCheckBoxChecked(int id)
    {
        Admin_Login adl = admin.GetAdmin(id);
        string purview = adl.Purview;
        string[] purArr = purview.Split(',');
        foreach (Control control in Panel1.Controls)
        {
            if (control is HtmlInputCheckBox)
            {
                for (int i = 0; i < purArr.Length; i++)
                {
                    if (purArr[i] == ((HtmlInputCheckBox)control).Value)
                    {
                        ((HtmlInputCheckBox)control).Checked = true;
                        break;
                    }
                }
            }
        }
        //foreach (Control control in Panel1.Controls)
        //{
        //    if (control is HtmlInputCheckBox)
        //    {
        //        ((HtmlInputCheckBox)control).Checked = true;
        //        foreach (string item in purArr)
        //        {
        //            if (((HtmlInputCheckBox)control).Value == item)
        //            {
        //                ((HtmlInputCheckBox)control).Checked = true;
        //                break;
        //            }
        //        }
        //        continue;
        //    }
        //}
    }
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <link href="../css/Admin_Style.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .red_font
        {
            color: #FF0000;
        }
    </style>
    <script type="text/javascript">
        function checkall(name, max) {
            var all = document.getElementById(name);
            for (var i = 1; i <= max; i++) {
                if (all.checked) {
                    var child = document.getElementById(name + i);
                    child.checked = true;
                } else {
                    var child = document.getElementById(name + i);
                    child.checked = false;
                }
            }
        }
        function userAdmin() {
            var obj = document.getElementById("AddID");
            if (/.*[\u4e00-\u9fa5]+.*$/.test(obj.value)) {
                alert('管理员登陆ID不能为中文');
                obj.value = "";
                obj.focus();
                return false;
            }
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div>
        <table width='100%' border='0' align='center' cellpadding='4' cellspacing='1' class='border'>
            <tr class='topbg'>
                <td colspan="2" align='center'>
                    <strong class="Glow">管 理 员 管 理</strong>
                </td>
            </tr>
            <tr class='tdbg'>
                <td width='70'>
                    <strong>管理导航：</strong>
                </td>
                <td>
                    <a href="?Action=Add">添加管理员</a> | <a href="AdminManager.aspx">管理员列表</a>
                </td>
            </tr>
        </table>
        <br />
        <asp:Panel ID="addP" runat="server" Width="100%" Visible="False">
            <table width='100%' border='0' align='center' cellpadding='4' cellspacing='1' class='border'>
                <tr class='tdbg'>
                    <td width="15%" class="tdbg2" align="right">
                        管理员ID：
                    </td>
                    <td width="85%">
                        <asp:TextBox ID="AddID" runat="server" MaxLength="10" onblur="userAdmin();" />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="AddID"
                            ErrorMessage="请输入管理员登录名"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr class='tdbg'>
                    <td class="tdbg2" align="right">
                        初始密码：
                    </td>
                    <td>
                        <asp:TextBox ID="AddPWD" runat="server" TextMode="Password" Width="127px" MaxLength="16" />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="AddPWD"
                            ErrorMessage="请输入密码"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr class='tdbg'>
                    <td class="tdbg2" align="right">
                        确认密码：
                    </td>
                    <td>
                        <asp:TextBox ID="AddPwd2" runat="server" TextMode="Password" Width="127px" MaxLength="16" />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="AddPwd2"
                            ErrorMessage="请输入密码"></asp:RequiredFieldValidator>
                        <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="AddPWD"
                            ControlToValidate="AddPwd2" ErrorMessage="两次输入的密码不一致"></asp:CompareValidator>
                    </td>
                </tr>
                <tr class='tdbg'>
                    <td class="tdbg2" align="right">
                        管理员姓名：
                    </td>
                    <td>
                        <asp:TextBox ID="AddName" runat="server" MaxLength="10" />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="请输入真实姓名"
                            ControlToValidate="AddName"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr class="tdbg">
                    <td class="tdbg2" align="right">
                        管理员邮箱地址：
                    </td>
                    <td>
                        <asp:TextBox ID="AddEMail" runat="server" MaxLength="30"></asp:TextBox>
                    </td>
                </tr>
            </table>
        </asp:Panel>
        <asp:Panel ID="editP" runat="server" Width="100%" Visible="False">
            <table width='100%' border='0' align='center' cellpadding='4' cellspacing='1' class='border'>
                <tr class='tdbg'>
                    <td width="15%" class="tdbg2" align="right">
                        管理员ID：
                    </td>
                    <td width="85%" valign="top">
                        <asp:TextBox ID="EditID" runat="server" Enabled="false" />
                    </td>
                </tr>
                <tr class='tdbg'>
                    <td width="15%" class="tdbg2" align="right">
                        原始密码：
                    </td>
                    <td width="85%" valign="top">
                        <asp:TextBox ID="OldEditPwd" runat="server" TextMode="Password" Width="127px" MaxLength="16" />
                        <asp:Label ID="EditLabel1" runat="server" ForeColor="Red"></asp:Label>
                    </td>
                </tr>
                <tr class='tdbg'>
                    <td class="tdbg2" align="right">
                        新密码：
                    </td>
                    <td>
                        <asp:TextBox ID="EditPWD" runat="server" TextMode="Password" Width="127px" MaxLength="16" />&nbsp;
                    </td>
                </tr>
                <tr class='tdbg'>
                    <td class="tdbg2" align="right">
                        确认新密码：
                    </td>
                    <td>
                        <asp:TextBox ID="EditPwd2" runat="server" TextMode="Password" Width="127px" MaxLength="16" />
                        <asp:CompareValidator ID="EditCV1" runat="server" ControlToCompare="EditPWD" ControlToValidate="EditPwd2"
                            ErrorMessage="两次输入的密码不一致"></asp:CompareValidator>
                    </td>
                </tr>
                <tr class="tdbg">
                    <td class="tdbg2" align="right">
                        管理员邮箱地址：
                    </td>
                    <td>
                        <asp:TextBox ID="EditEmail" runat="server" Width="127px" MaxLength="16" />
                    </td>
                </tr>
            </table>
        </asp:Panel>
        <asp:Panel ID="Panel1" runat="server" Width="100%" Visible="false">
            <br />
            <table width='100%' border='0' align='center' cellpadding='4' cellspacing='1' class='border'>
                <tr class='topbg'>
                    <td colspan="2" align='center'>
                        <strong class="Glow">管 理 员 权 限 管 理</strong>
                    </td>
                </tr>
                <tr class='tdbg'>
                    <td align="right" class="tdbg2">
                        个人管理权限
                    </td>
                    <td>
                        <label>
                            <input id="person1" type="checkbox" value="2001" runat="server" />个人用户管理、求职管理【包括简历屏蔽】、个人身份转换【包括普通人才、推荐人才】</label><br />
                        <label>
                            <input id="person2" type="checkbox" value="2002" runat="server" />修改个人资料【修改密码】</label><br />
                        <label>
                            <input id="person3" type="checkbox" value="2003" runat="server" />个人账号管理【包括账号冻结】</label><br />
                    </td>
                </tr>
                <tr class='tdbg'>
                    <td align="right" class="tdbg2">
                        企业管理权限
                    </td>
                    <td>
                        <label>
                            <input id="company1" type="checkbox" value="2101" runat="server" />查看企业列表、企业身份转换【包括普通企业、推荐企业】</label><br />
                        <label>
                            <input id="company2" type="checkbox" value="2102" runat="server" />修改企业资料【包括密码】</label><br />
                        <label>
                            <input id="company3" type="checkbox" value="2103" runat="server" />删除企业资料</label><br />
                    </td>
                </tr>
                <tr class='tdbg'>
                    <td align="right" class="tdbg2">
                        管理员管理权限<br />
                        <span class="STYLE4"></span>
                    </td>
                    <td>
                        <label>
                            <input id="admin1" type="checkbox" value="2201" runat="server" />查看管理员列表【不勾选这项，登陆者只能查看自己的列表】</label><br />
                        <label>
                            <input id="admin2" type="checkbox" value="2202" runat="server" />添加管理员资料【新增管理员并赋予其相应的权限】</label><br />
                        <label>
                            <input id="admin3" type="checkbox" value="2203" runat="server" />修改管理员资料【如不勾选"查看列表选项"，则只能修改自己的信息，但不能修改权限】</label><br />
                        <label>
                            <input id="admin4" type="checkbox" value="2204" runat="server" />删除管理员资料</label>
                    </td>
                </tr>
                <tr class='tdbg'>
                    <td align="right" class="tdbg2">
                        高级信息管理权限
                    </td>
                    <td>
                        <label>
                            <input id="sjk1" type="checkbox" value="2301" runat="server" />查看高级信息</label><br />
                        <label>
                            <input id="sjk2" type="checkbox" value="2302" runat="server" />操作高级信息</label>
                    </td>
                </tr>
            </table>
            <div align="center">
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="确定" />
                <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="修改" />
                <input type="reset" name="Submit" value="重置" />
            </div>
            <br />
            <asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label>
        </asp:Panel>
        <asp:Panel ID="manageP" runat="server" Width="100%" Visible="False">
            <table width='100%' border='0' align='center' cellpadding='2' cellspacing='1' class='border'>
                <tr class='topbg2'>
                    <td width="7%" align='center' style="height: 24px">
                        <strong class="Glow">序号</strong>
                    </td>
                    <td width="18%" align='center' style="height: 24px">
                        <strong class="Glow">管理员ID</strong>
                    </td>
                    <td width="12%" align='center' style="color: #000000; height: 24px">
                        <strong class="Glow">昵称</strong>
                    </td>
                    <td width="8%" align='center' style="height: 24px">
                        <strong class="Glow">状态</strong>
                    </td>
                    <td width="19%" align='center' style="height: 24px">
                        <strong class="Glow">操作</strong>
                    </td>
                </tr>
                <asp:Repeater ID="RP1" runat="server">
                    <ItemTemplate>
                        <tr class='tdbg'>
                            <td align='center'>
                                <%#Eval("Id") %>
                            </td>
                            <td align='center'>
                                <%#Eval("LoginName")%>
                            </td>
                            <td align='center'>
                                <%#Eval("RealName") %>
                            </td>
                            <td align='center'>
                                <%#sf() %>
                            </td>
                            <td align='center'>
                                <a href="AdminManager.aspx?id=<%#Eval("Id")%>&amp;Action=Edit">修改</a> | <a href="AdminManager.aspx?id=<%#Eval("Id") %>&amp;Action=Del"
                                    onclick="return confirm('确定要删除此项吗？此操作不可逆。');">删除</a>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </table>
            <br />
            <span class="red_font">备注</span>：超级管理员进入修改，可以设置该管理员的各项操作权限。
        </asp:Panel>
    </div>
</asp:Content>
